package com.vehicle.controller;

import com.vehicle.bean.DataGridView;
import com.vehicle.bean.ResultDto;
import com.vehicle.pojo.Car;
import com.vehicle.pojo.Insurance;
import com.vehicle.pojo.InsuranceRecord;
import com.vehicle.service.InsuranceRecordService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value ="/insuranceRecord")
public class InsuranceRecordController extends BaseController{
    private Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private InsuranceRecordService insuranceRecordService;
    @RequestMapping(value ="/insuranceRecords")
    @ResponseBody
    public DataGridView insuranceRecordList(InsuranceRecord insuranceRecord,Integer carid,String itype){
        //数据处理
        Car car = new Car();
        car.setCarid(carid);
        Insurance insurance = new Insurance();
        insurance.setItype(itype);
        insuranceRecord.setInsurance(insurance);
        insuranceRecord.setCar(car);

        return insuranceRecordService.findInsuranceRecordByPage(insuranceRecord);
    }
    @ResponseBody
    @RequestMapping(value = "/deleteInsuranceRecord")
    public ResultDto deleteInsuranceRecord(Integer insuranceid){
        ResultDto resultDto = new ResultDto();
        try {
            insuranceRecordService.deleteInsuranceRecordByid(insuranceid);
            resultDto.setCode(200);
            resultDto.setMsg("删除成功");
        } catch (Exception e) {
            resultDto.setCode(-200);
            resultDto.setMsg("删除失败");
            e.printStackTrace();
        }
        return resultDto;
    }
    @ResponseBody
    @RequestMapping(value = "/addInsuranceRecord")
    public ResultDto addInsuranceRecord(InsuranceRecord insuranceRecord){
        ResultDto resultDto = new ResultDto();
        try {
            insuranceRecordService.addInsuranceRecord(insuranceRecord);
            resultDto.setCode(200);
            resultDto.setMsg("增加成功");
        } catch (Exception e) {
            resultDto.setCode(-200);
            resultDto.setMsg("增加失败");
            e.printStackTrace();
        }
        return resultDto;
    }
    @ResponseBody
    @RequestMapping(value = "/updateInsuranceRecord")
    public ResultDto updateInsuranceRecord(InsuranceRecord insuranceRecord){
        ResultDto resultDto = new ResultDto();
        try {
            insuranceRecordService.updateInsuranceRecord(insuranceRecord);
            resultDto.setMsg("修改成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            resultDto.setMsg("修改失败");
            resultDto.setCode(-200);
            e.printStackTrace();
        }
        return resultDto;
    }
    @ResponseBody
    @RequestMapping(value = "/deleteBatchInsuranceRecord")
    public ResultDto deleteBatchInsuranceRecord(@RequestParam("insuranceIds[]") Integer[] insuranceIds){
        ResultDto resultDto = new ResultDto();
        try {
            insuranceRecordService.deleteBatchInsuranceRecordByIds(insuranceIds);
            resultDto.setMsg("删除成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            resultDto.setMsg("删除失败");
            resultDto.setCode(-200);
            e.printStackTrace();
        }finally {
            return resultDto;
        }
    }
}
